Skip to content

BoolDictToolButton

Base classes

Name Children Inherits
ToolButton
prettyqt.widgets.toolbutton
Quick-access button to commands or options, usually used inside a QToolBar.

⋔ Inheritance diagram

graph TD
  2668692751["custom_widgets.BoolDictToolButton"]
  2322451829["widgets.ToolButton"]
  2670709832["widgets.AbstractButtonMixin"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  902471069["QtWidgets.QToolButton"]
  4168111313["QtWidgets.QAbstractButton"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  2322451829 --> 2668692751
  2670709832 --> 2322451829
  829357528 --> 2670709832
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  2751680898 --> 2670709832
  902471069 --> 2322451829
  4168111313 --> 902471069
  3620195106 --> 4168111313
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: ToolButton

Source code in prettyqt/custom_widgets/booldicttoolbutton.py
class BoolDictToolButton(widgets.ToolButton):
    value_changed = core.Signal(dict)

    def __init__(
        self,
        *args,
        dct: dict[str, str] | None = None,
        **kwargs,
    ):
        super().__init__(*args, **kwargs)
        self.button_menu = widgets.Menu(triggered=self._on_menu_click)
        self.setMenu(self.button_menu)
        self.set_popup_mode("instant")
        if dct:
            self.set_dict(dct)

    def _on_menu_click(self):
        self.value_changed.emit(self.as_dict())

    def __getitem__(self, key: str) -> bool:
        return self.button_menu[key].isChecked()

    def __setitem__(self, key: str, value: bool):
        self.button_menu[key].setChecked(value)
        self.value_changed.emit(self.as_dict())

    def set_dict(self, dct: dict[str, str]):
        self.button_menu.clear()
        for k, v in dct.items():
            action = gui.Action(text=v, checkable=True, object_name=k)
            self.button_menu.add(action)
        self.value_changed.emit(self.as_dict())

    def as_dict(self) -> dict[str, bool]:
        return {act.objectName(): act.isChecked() for act in self.button_menu}

:fa-brands-github: Show source on GitHub